import { useEffect } from 'react'
import { useLocation, useNavigate } from 'react-router-dom'
import useGetUserInfo from './useGetUserInfo'
import { isLoginOrRegister, isNoNeedUserInfo, MANAGE_INDEX_PATHNAME, LOGIN_PATHNAME } from '../router'

function useNavPage(waitingUserData: boolean) {
  const { username } = useGetUserInfo()
  const { pathname } = useLocation()
  const nav = useNavigate()
  useEffect(() => {
    if (waitingUserData) return
    if (username) { // 登录了
      if (isLoginOrRegister(pathname)) {
        nav(MANAGE_INDEX_PATHNAME)
      }
      return
    }
    if (!isNoNeedUserInfo(pathname)) {
      nav(LOGIN_PATHNAME)
    }

  }, [username, pathname, waitingUserData])
}
export default useNavPage